package com.shuaiqidefeng.PDFToJPG;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;


/**
 * 返回转换后的文件夹
 */
public class PDFTool {
    //判断是否为PDF文件
    public Result isPDFFile(String fileName) {
        if (fileName != null && !fileName.trim().isEmpty()) {
            String[] split = fileName.split("\\.");
            String type = split[split.length - 1];
            if (type.equals("PDF") || type.equals("pdf")) {
                Result result = new Result("为PDF文件", true, null);
                return result;
            }
            return new Result("不为PDF文件", false, null);
        }
        return new Result("文件名为空", false, null);
    }

    //转换JPG
    public Result pdfbox(File file) {
        PDDocument doc = null;
        ByteArrayOutputStream os = null;
        InputStream stream = null;

        OutputStream out = null;

        String outPutFilePath = file.getPath().replace(".pdf", "");
        File file1 = new File(outPutFilePath);
        file1.mkdirs();

        /*InputStream is = null;
        OutputStream responseOut = null;*/
        try {
            long start = System.currentTimeMillis();
            //pdf路径
            stream = new FileInputStream(file);
            // 加载解析PDF文件
            doc = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            PDPageTree pages = doc.getPages();

            String fileName = file.getName();

            //获取文件去掉文件类型的名字
            String fileRealName = fileName.replace(".pdf", "");

            int pageCount = pages.getCount();


            int l = 0;

            while (l < pageCount) {
                System.out.println("processing page:" + (l+1));
                BufferedImage bim = pdfRenderer.renderImageWithDPI(l, 300);
                //System.out.println("a");
                os = new ByteArrayOutputStream();
                //System.out.println("b");
                ImageIO.write(bim, "jpg", os);
                //System.out.println("c");
                byte[] datas = os.toByteArray();

                l++;
                //**jpg文件转出路径的文件夹
                //创建文件夹
                //System.out.println("c");
                out = new FileOutputStream(outPutFilePath + "\\" + fileRealName + " (" + (l) + ")" + ".jpg");
                //System.out.println("d");
                out.write(datas);
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            long end = System.currentTimeMillis();
            long time = (end - start);
            System.out.println("pdf to jpg used： " + time);

        } catch (Exception e) {
            return new Result("转换失败", false, null);
        } finally {
            if (doc != null) {
                try {
                    doc.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("qqq");
        return new Result("成功转为JPG", true, file1);
    }
}
